<template>
  <page-layout>
    <div slot="toolbar">
      <el-button size="small" round @click="goBack">返回</el-button>
    </div>
    <div class="card-header m_10">中控室管理</div>
    <el-card class="box-card m_10">
            <el-card>
              <el-form inline>
                <el-form-item label="所属企业">
                  <el-select v-model="searchCondition.deptId" placeholder="请选择">
                    <el-option
                      v-for="item in departData"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="中控室名称">
                  <el-input v-model="searchCondition.name"></el-input>
                </el-form-item>

                <el-form-item>
                  <el-button size="small" type="primary" @click="refresh">查询</el-button>
                  <el-button size="small" type="primary" @click="newRoom">新增中控室</el-button>
                </el-form-item>
              </el-form>
            </el-card>
            <el-table
              :data="rooms.content" border stripe
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="110">
              </el-table-column>
              <el-table-column prop="deptName" label="所属企业" width="150"></el-table-column>
              <el-table-column prop="name" label="名称" width="120"></el-table-column>
              <el-table-column label="操作" width="180">
                <template slot-scope="scope">
                  <el-button size="mini" @click="downloadCode(scope.row)" type="text">下载二维码</el-button>
                  <el-button size="mini" @click="editRoom(scope.$index, scope.row)" type="text">编辑</el-button>
                  <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="rooms.totalElements"
                :page-size="rooms.size"
                :current-page.sync="currentAdminPage" @current-change="adminPageChange">
              </el-pagination>
            </div>
    </el-card>
    <el-dialog :visible.sync="showAdminEditor" title="添加/编辑 中控室" width="50%">
      <room-editor @close="onAdminEditorClose" :is-close="showAdminEditor"></room-editor>
    </el-dialog>
  </page-layout>
</template>
<script>
import { mapGetters } from "vuex";
import RoomEditor from "./room-editor";
import PageLayout from "@/components/page-layout";
import QRCode from "qrcode";
export default {
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.departId = to.query.departId;
      vm.departName = to.query.departName;
      vm.refresh();
    });
  },
  created() {
    this.$store.dispatch("getDepartListS").then(res => {});
  },
  mounted() {},
  data() {
    return {
      action: "add",
      searchCondition: { name: "", deptId: "" },
      currentAdminPage: 1,
      departId: "",
      departName: "",
      showAdminEditor: false,
      passRules: {
        password: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 8 到 15 个字符", trigger: "blur" }
        ]
      },
      config: {
        value: "111111" //显示的值、跳转的地址
      }
    };
  },
  computed: {
    ...mapGetters({
      areaTree: "areaTree",
      departList: "departList",
      rooms: "rooms"
    }),
    departData() {
      var tem = this.departList;
      tem.unshift({ name: "全部企业", id: "" });
      return tem;
    }
  },
  methods: {
    createCode(text) {},
    goBack() {
      this.$router.push({ name: "department" });
    },
    downloadCode(row) {
      QRCode.toDataURL(row.id + "", { version: 5 })
        .then(url => {
          var a = document.createElement("a");
          var event = new MouseEvent("click");
          // 下载图名字
          a.download = row.name;
          //url
          a.href = url;
          //合成函数，执行下载
          a.dispatchEvent(event);
        })
        .catch(err => {
          console.error(err);
        });
    },
    onAdminEditorClose() {
      this.showAdminEditor = false;
      this.adminPageChange(1);
    },
    newRoom() {
      this.$store.commit("INIT_CURRENT_ROOM", {
        departId: this.departId,
        departName: this.departName
      });
      this.showAdminEditor = true;
    },
    editRoom(index, row) {
      this.$store.dispatch("getControlRoomS", row.id);
      this.showAdminEditor = true;
    },

    adminPageChange(val) {
      this.searchCondition.deptId = this.departId;
      this.$store
        .dispatch(
          "getControlRoomPageS",
          Object.assign(this.searchCondition, {
            pageIndex: val - 1
          })
        )
        .then(ret => {});
    },
    //移除未保存数据
    handleDelete(row) {
      this.$confirm("确认删除该中控室吗?", "提示", {})
        .then(() => {
          this.$store
            .dispatch("deleteControlRoomS", { id: row.id })
            .then(ret => {
              this.refresh({ pageIndex: 0 });
              this.$notify({
                title: "删除成功",
                type: "success"
              });
            });
        })
        .catch(() => {});
    },
    refresh() {
      this.adminPageChange(1);
    }
  },
  components: { RoomEditor, PageLayout }
};
</script>
<style lang="less">
.m_10 {
  margin: 10px;
}
.m_t_10 {
  margin-top: 10px;
}
.tree_edit {
  float: right;
}
.line {
  border: 0.5px solid #ebeef5;
  margin-top: 20px;
  margin-bottom: 10px;
}
</style>
